package collection;

import java.util.*;

/**
 * Collection的常用方法
 * add():   向集合中添加一个元素
 * size():  查看一个集合中的元素个数
 * isEmpty():查看集合是否为空集，仅当size()为0时返回true
 * clear():清空集合
 * contains():  判断集合中是否包含某元素(equals()相等即为true)
 * remove():    将集合中删除给定元素(equals()相等即为true)成功删除则返回true
 * addALL():    将参数集合中的元素添加到当前集合中，当前集合放生改变则返回true
 * containsAll():判断当前集合是否包含参数集合中的所元素
 * retainAll():取交集
 * removeAll():删交集
 * iterator():获取一个用于遍历当前集合元素的迭代器
 * toArray():将集合转为数组
 * */
public class CollectionDemo01 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        //add()
        c.add("Monday");
        c.add("Tuesday");
        c.add("Wednesday");
        c.add("Thursday");
        c.add("Friday");
        c.add("Saturday");
        c.add("Sunday");
        System.out.println("初始集合为:"+c);//[Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday]
        //size()
        System.out.println("初始集合的元素个数为:"+c.size());//7
        //isEmpty()
        boolean empty = c.isEmpty();
        System.out.println(empty);//f
        //clear()
        c.clear();
        System.out.println("清空集合");
        System.out.println(c);//此时集合为空[]
        c.add("Monday");
        c.add("Tuesday");
        c.add("Wednesday");
        c.add("Thursday");
        c.add("Friday");
        c.add("Saturday");
        c.add("Sunday");
        System.out.println("重新添加后集合为:"+c);
        //contains()判断当前集合中是否存在Monday
        boolean empty1 = c.contains("Monday");
        System.out.println(empty1);//拥有Monday，输出T
        //remove():
        boolean empty2 = c.remove("Monday");
        System.out.println(empty2);//删除了Monday，返回T
        System.out.println("删除Monday后的集合为:"+c);//[Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday]
        //addALL()
        Collection<String> cc = new ArrayList<>();
        cc.add("Monday");
        cc.add("weekend");
        System.out.println("新的cc集合为:"+cc);//[Monday,weekend]
        c.addAll(cc);
        System.out.println("将cc新集合加入c集合后,c集合为:"+c);
        //containsAll()  判断c集合中是否包含cc集合中的元素
        boolean emptyAll = c.containsAll(cc);
        System.out.println(emptyAll);//T
        //retainAll()  取cc集合与c集合的交集
        //c.retainAll(cc);
        //System.out.println("c中仅包含两个集合的并集，此时的c集合为:"+c);//[Monday, weekend]
        // removeAll()
        cc.add("Tuesday");
        cc.add("Wednesday");
        System.out.println(cc);
        c.removeAll(cc);
        System.out.println("删除两个集合中的交集后c变为:"+c);//[Thursday, Friday, Saturday, Sunday]

        /**
         * iterator():获取一个用于遍历当前集合元素的迭代器
         * 加强for循环
         */
        Collection<String> c1 = new ArrayList<>();
        c1.add("Monday");
        c1.add("Tuesday");
        c1.add("Wednesday");
        c1.add("Thursday");
        c1.add("Friday");
        System.out.println("即将要遍历的集合c1为:"+c1);
        Iterator it =  c1.iterator();//获取c1的迭代器
        while (it.hasNext()){
            String str =(String) it.next();
            System.out.println(str);
            if ("#".equals(str)){
                //c.remove(str);//迭代器遍历过程中不允许通过集合的方式来增删元素
                it.remove();//删除next()方法所获取的元素
            }
        }//迭代器遍历
        System.out.println(c1);
        //加强for循环
        for (Object str1 : c1){
            String str = (String)str1;
            System.out.println(str);
        }

        //集合与数组的互转:
        //集合转化为数组
        String[] array = c1.toArray(new String[5]);
        System.out.println("转化后的数组为:"+Arrays.toString(array));
        //数组转化为集合
        String[] array1 = {"one","two","three","four","five"};
        System.out.println("array1:"+ Arrays.toString(array));
        List<String> list = Arrays.asList(array);
        System.out.println("转化后的集合为:"+list);

    }
}
